<template>
  <div class="SignUp">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('CreatAccount')" left-arrow fixed @click-left="onClickLeft" />

    <van-form name="addSite" @submit="addUser">
      <div class="registered_box">
        <div class="registered_li">
          <div class="registered_li_tit required">{{$t('LoginAccount')}}</div>
          <div class="registered_li_ipt">
            <van-field clearable name="loginName" v-model="loginName" type="text" />
          </div>
        </div>
        <div class="registered_li">
          <div class="registered_li_tit required">{{$t('Password')}}</div>
          <div class="registered_li_ipt">
            <van-field clearable name="password" v-model="password" type="password" />
          </div>
        </div>
        <div class="registered_li">
          <div class="registered_li_tit required">{{$t('Nation')}}</div>
          <div class="registered_li_ipt" @click="showNation">
              <van-field readonly clickable name="country" v-model="country" type="text" />
          </div>
          <nationList ref="nation" @getNation="getNation"></nationList>
        </div>
        <div class="registered_li">
          <div class="registered_li_tit" :class="{'required':notChina}">{{$t('Email')}}</div>
          <div class="registered_li_ipt">
            <van-field clearable name="email" v-model="email" type="text" />
          </div>
        </div>
      </div>
      <div class="addSite_Agreement">
        <van-checkbox class="authorCheck" v-model="authorizeFlag" checked-color="#F39800"></van-checkbox>
        <span @click="openProtocol">{{$t('RegisterAgree')}}</span>
      </div>
      <div class="btnBox">
        <van-button color="linear-gradient(#F9C25F, #F5861A)" :disabled="!authorizeFlag||loginName==''||password==''||country==''||(notChina&&email=='')" round block native-type="submit">{{$t('CreatAccount')}}</van-button>
      </div>
    </van-form>
    <protocol ref="proShow"></protocol>
  </div>
</template>

<script>
import md5 from 'js-md5'
import moment from 'moment'
import nationList from '../../components/IndexBar/NationList'
import protocol from '../../components/Protocol/Protocol'
import { Dialog } from 'vant'

export default {
  components:{
    nationList,
    protocol
  },
  data(){
    return {
      authorizeFlag:true,
      loginName:'',
      password:'',
      columns:[],
      country:'',
      email:'',
      latitude:'',
      longitude:'',
      notChina:true
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:'/scan'});
    },
    showNation(){
      this.$refs.nation.openNation(true);
    },
    getNation(data){
      this.country=data;
      if(data!="中国"){
        this.notChina=true;
      }else{
        this.notChina=false;
      }
    },
    openProtocol(){
      this.$refs.proShow.openPro(true);
    },
    //自动获取国家
    geolocation(){
      navigator.globalization.getLocaleName((data)=>{
        let codeArr=data.value.split('-');
        this.codeForNation(codeArr[1]);
      });
    },
    //根据code获取国家
    async codeForNation(code){
      let res = await this.API.selectCountry(1,{});
      res.forEach(element => {
        if(element.enShort==code){
          this.country=element.enName;
          if(this.country!="中国"){
            this.notChina=true;
          }else{
            this.notChina=false;
          }
        }
      });
    },
    async appRegisterOnly(code){
      this.comm.loadingShow();
      let res = await this.API.appRegisterOnly(code,{
        'loginName':encodeURI(this.loginName),
        'password':md5(this.password),
        'sn':localStorage.getItem('wifiSN'),
        'country':encodeURI(this.country),
        'email':this.email||'',
        'agreementFlag':1,
        'websiteType':0,
        'agreeTime':moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
      });
      if(res.exception == 3){
        localStorage.setItem("domain",code);
        localStorage.setItem('userId',res.result.ids);
        Dialog.alert({
          title: this.$t('Tip'),
          message: this.$t('RegisterSuccess'),
          confirmButtonText:this.$t('Next')
        }).then(() => {
          this.$router.push({path:'/signtwo',query:{'nation':this.country}});
        });
      }else if(res.exception == 1){
        this.comm.tipsShow('SNinvalid');
      }else if(res.exception == 2){
        this.comm.tipsShow('UserAndPwdExist');
      }else if(res.exception == 0){
        localStorage.setItem("domain",0);
        this.appRegisterOnly(0);
      }else{
        this.comm.tipsShow('SystemException');
      }
    },
    addUser(values){
      let regEmail=/^[A-Za-z0-9_-\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      let regLog=/^[a-zA-Z0-9@._-]+$/;
      if(!regLog.test(values.loginName)||!regLog.test(values.password)){
        this.comm.tipsShow('LogAndPwdError');
      }else if(!regEmail.test(values.email)&&values.email!=""){
        this.comm.tipsShow('EmailError');
      }else{
        this.appRegisterOnly(1);
      }
    }
  },
  mounted(){
    this.geolocation();
  }
}
</script>

<style lang="less">
.SignUp{
  .registered_box{
    width: 100%;
    background: #ffffff;
  }
  .registered_li{
    width: 100%;
    border-bottom: 1px solid #efefef;
  }
  .registered_li_tit{
    padding: 12px 0 0 15px;
    width: 100%;
    color: #929292;
    text-align: left;
    box-sizing: border-box;
  }
  .siteInfo{
    width: 100%;
    background: #ffffff;
    border-bottom: 1px solid #efefef;
  }
  .siteInfo_t{
    width:100%;
    height: 35px;
    padding: 8px;
    box-sizing: border-box;
  }
  .addSiteType_box{
    width: 100%;
    height: 45px;
    text-align: left;
    padding-left: 10px;
    border-bottom: 1px solid #efefef;
    box-sizing: border-box;
  }
  .addSiteType_box_l{
    float: left;
    width: 127px;
    height: 45px;
    line-height: 45px;
    font-size: 14px;
  }
  .addSiteType_box_r{
    float: left;
    width: 180px;
    height: 44px;
    line-height: 44px;
  }
  .inner{
    width: 90px;
    float: left;
    height: 44px;
    line-height: 44px;
  }
  .addSite_Agreement{
    text-decoration: underline;
    padding: 10px;
    display: flex;
    align-items:center;
    text-align: left;
    font-size: 13px;
    color: #ED7523;
    .authorCheck{
      padding-right: 10px;
    }
  }
}
</style>
